<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\jujin\controller;

use controller\JunjinAdmin;
use think\Db;

/**
 * Class Article
 * @package app\jujin\controller
 */
class Goods extends JunjinAdmin
{

    /**
     * 定义当前操作表名
     * @var string
     */
    public $table = 'SupplierGoods';

    /**
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->title = '产品列表';
        $get = $this->request->get();
        $db = Db::name($this->table);
        if (isset($get['mid']) && is_numeric($get['mid'])) {
            $db->where('mid', $get['mid']);
        }
        if (isset($get['name']) && $get['name']!='') {
            $db->whereLike('name', '%'.$get['name'].'%');
        }
        $this->_form_assign();
        return parent::_list($db->order('sort,id'));
    }

    public function _data_filter(&$list)
    {
        foreach ($list as $k=>&$v){
            $v['mid']=Db::name('supplier_info')->where('mid',$v['mid'])->value('name');
        }
    }

    private function _form_assign()
    {
        //查询所有供应商
        $this->assign('suppliers',Db::name('supplier_info')->field('mid,name')->select());
    }

    protected function _form_filter(&$data)
    {
        if($this->request->isGet()){
            if($this->request->get('id')){
                $this->title='编辑产品';
            }else{
                $this->title='添加产品';
            }
            $this->_form_assign();
        }else{
            if(!$this->request->post('id')){
                $data['status']=1;
            }
        }
    }

}
